﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using JobBoardAdminPanel.Helpers;
using JobBoardAdminPanel.Commands;
using System.IO;

namespace JobBoardAdminPanel.ViewModels
{
    public class EditCompaniesViewModel : ViewModelBase
    {
        private ObservableCollection<CompanyViewModel> companies;
        private CompanyViewModel currentCompany;
        private string currentPicturePath;
        private CompanyViewModel newCompany;
        private SaveCompanyCommand saveCommand;
        private DeleteCompanyCommand deleteCommand;
        private AddCompanyCommand addCommand;

        public EditCompaniesViewModel()
        {
            RefreshCompanies();
            this.saveCommand = new SaveCompanyCommand(this);
            this.deleteCommand = new DeleteCompanyCommand(this);
            this.addCommand = new AddCompanyCommand(this);
            newCompany = new CompanyViewModel(new JobsBoardDAL.DTOs.CompanyDTO());
        }
  
        public CompanyViewModel NewCompany
        {
            get
            {
                return newCompany;
            }
            set
            {
                if (this.newCompany != value)
                {
                    newCompany = value;
                    OnPropertyChanged("NewCompany");
                }
            }
        }

        private void RefreshCompanies()
        {
            Companies = new ObservableCollection<CompanyViewModel>(ViewModelsHelper.GetAllCompanies());
        }

        public SaveCompanyCommand SaveCommand
        {
            get
            {
                return saveCommand;
            }
            set
            {
                saveCommand = value;
            }
        }

        public DeleteCompanyCommand DeleteCommand
        {
            get
            {
                return deleteCommand;
            }
            set
            {
                deleteCommand = value;
            }
        }

        public AddCompanyCommand AddCommand
        {
            get
            {
                return addCommand;
            }
            set
            {
                addCommand = value;
            }
        }

        public string CurrentPicturePath
        {
            get
            {
                return currentPicturePath;
            }
            set
            {
                currentPicturePath = value;
            }
        }

        public CompanyViewModel CurrentCompany
        {
            get
            {
                return currentCompany;
            }
            set
            {
                if (this.currentCompany != value)
                {
                    currentCompany = value;
                    OnPropertyChanged("CurrentCompany");
                }
            }
        }

        public ObservableCollection<CompanyViewModel> Companies
        {
            get
            {
                return companies;
            }
            set
            {
                if (this.companies != value)
                {
                    companies = value;
                    OnPropertyChanged("Companies");
                }
            }
        }

        internal void Add()
        {
            if (!String.IsNullOrEmpty(CurrentPicturePath))
            {
                byte[] bytes = GetLogoData();
                this.NewCompany.Logo = bytes;
            }

            try
            {
                this.NewCompany.Add();
                RefreshCompanies();
                this.NewCompany = new CompanyViewModel(new JobsBoardDAL.DTOs.CompanyDTO());
            }
            catch
            {
            }
        }

        internal void Update()
        {
            if (!String.IsNullOrEmpty(CurrentPicturePath))
            {
                byte[] bytes = GetLogoData();
                this.CurrentCompany.Logo = bytes;
            }

            if (this.CurrentCompany != null)
            {
                this.CurrentCompany.Update();
                RefreshCompanies();
            }
        }
  
        private byte[] GetLogoData()
        {
            byte[] bytes = null;

            using (FileStream file = File.Open(CurrentPicturePath, FileMode.Open))
            {
                bytes = new byte[file.Length];
                long bytesToRead = file.Length;
                long bytesRead = 0;

                while (bytesToRead > 0)
                {
                    int n = file.Read(bytes, (int)bytesRead, (int)bytesToRead);
                    if (n == 0)
                        break;

                    bytesRead += n;
                    bytesToRead -= n;
                }
            }
            return bytes;
        }

        internal void Delete()
        {
            this.CurrentCompany.Delete();
            RefreshCompanies();
        }
    }
}
